package com.ys.comparatordemo;

import java.util.Comparator;

/**
 * 描述: TODO
 *
 * @author CodeYang
 * @version 2020/12/18 21:24
 */
public class OrderByNameDesc implements Comparator {
    /**
     * 根据姓名进行排序
     * @param o1
     * @param o2
     * @return
     */
    @Override
    public int compare(Object o1, Object o2) {
        // 1向下转型
        if (o1 instanceof Student && o2 instanceof Student){
            Student stu1 = (Student) o1;
            Student stu2 = (Student) o2;
            int flag = stu1.getName().compareTo(stu2.getName());
            // 降序排序
            return flag == 0 ? 0 : flag > 0 ? -1 : 1;
        }
        // 执行到此处，证明obj1或obj2不是Student类型的实例
        throw new IllegalArgumentException("obj1或obj2不是Student类的实例！");
    }
}
